package pl.edu.pw.MidEmu.UI.Components;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import pl.edu.pw.MidEmu.Base.Components.BaseFrame;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.Common.Models.EthernetInterfaceModel;
import pl.edu.pw.MidEmu.UI.Controllers.NetworkSettingsController;
import pl.edu.pw.MidEmu.UI.Views.NetworkSettingsView;

/**
 * Frame with GUI for managing network interfaces
 * @author Mateusz Starodab
 *
 */
public class NetworkSettingsFrame extends BaseFrame<JDialog, NetworkSettingsController> implements NetworkSettingsView
{
	private JButton btnAddNew;

	private JButton btnCancel;
	private JButton btnRefresh;
	private MainFrame parent;
	private JPanel pnlCenter;
	private JPanel pnlMain;
	private JPanel pnlSouth;

	public NetworkSettingsFrame(MainFrame mainFrame)
	{
		this.parent = mainFrame;

		NetworkSettingsController controller = new NetworkSettingsController();
		controller.setView(this);
		setController(controller);
		JDialog frame = new JDialog();
		setComponent(frame);

		getComponent().setTitle(Consts.MainFrame_Title + " - " + Consts.NetworkSettings_Title);
		getComponent().setSize(230, 370);
		getComponent().setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		getComponent().setModal(true);

		pnlMain = new JPanel(new BorderLayout());

		pnlCenter = new JPanel(new FlowLayout(FlowLayout.LEFT));
		pnlCenter.setPreferredSize(new Dimension(200, 400));
		pnlCenter.setBorder(BorderFactory.createTitledBorder(Consts.NetworkSettings_InterfacesLabel));

		List<EthernetInterfaceModel> ethernetModels = getController().getEthernetInterfaces();
		for (EthernetInterfaceModel ethernetModel : ethernetModels)
		{
			JCheckBox box = new JCheckBox(ethernetModel.toString());
			box.setName(ethernetModel.getName());
			box.addActionListener(getController());
			box.getModel().setSelected((parent.getController().checkIfIfaceIsUsed(ethernetModel.getName())));
			pnlCenter.add(box);
		}

		pnlMain.add(pnlCenter, BorderLayout.CENTER);

		pnlSouth = new JPanel(new FlowLayout(FlowLayout.CENTER));

		btnAddNew = new JButton(Consts.NetworkSettings_ButtonAdd);
		btnAddNew.setName(Consts.NetworkSettings_ButtonAdd);
		btnAddNew.addActionListener(getController());
		pnlSouth.add(btnAddNew);

		btnRefresh = new JButton(Consts.NetworkSettings_ButtonRefresh);
		btnRefresh.setName(Consts.NetworkSettings_ButtonRefresh);
		btnRefresh.addActionListener(getController());
		pnlSouth.add(btnRefresh);

		btnCancel = new JButton(Consts.NetworkSettings_ButtonCancel);
		btnCancel.setName(Consts.NetworkSettings_ButtonCancel);
		btnCancel.addActionListener(getController());
		pnlSouth.add(btnCancel);

		pnlMain.add(pnlSouth, BorderLayout.SOUTH);

		getComponent().setContentPane(pnlMain);
	}

	@Override
	public void dispose()
	{
		getComponent().setVisible(false);
		getComponent().dispose();
	}

	@Override
	public void refresh()
	{
		pnlCenter.removeAll();

		List<EthernetInterfaceModel> ethernetModels = getController().getEthernetInterfaces();
		for (EthernetInterfaceModel ethernetModel : ethernetModels)
		{
			JCheckBox box = new JCheckBox(ethernetModel.toString());
			box.setName(ethernetModel.getName());
			box.addActionListener(getController());
			box.getModel().setSelected((parent.getController().checkIfIfaceIsUsed(ethernetModel.getName())));
			pnlCenter.add(box);
		}

		getComponent().validate();
	}

	@Override
	public void registerIface(EthernetInterfaceModel name)
	{
		parent.getController().registerIface(name);
	}

	@Override
	public void unregisterIface(String name)
	{
		parent.getController().unregisterIface(name);
	}

}
